package com.probeez.profiles.gsensor;

import static com.probeez.profiles.gsensor.EvalConditionService.DEBUG;
import static com.probeez.profiles.gsensor.EvalConditionService.STATE_POSITION;
import static com.probeez.profiles.gsensor.EvalConditionService.TAG;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.probeez.profiles.plugin.PluginBroadcastReceiver;

/**
 * Class to listen for SP intent broadcasts.
 * It should be declared in <code>AndroidManifest.xml</code> with appropriate intent filter. 
 */
public class PluginController extends PluginBroadcastReceiver {

	@Override
	protected boolean onCheckCondition(Context context, Bundle sourceState, Bundle targetState) {
		if (sourceState==null || targetState==null) {
			return false;
		}
		int srcPos = sourceState.getInt(STATE_POSITION, -1);
		int dstPos = targetState.getInt(STATE_POSITION, -1);
		if (DEBUG) Log.d(TAG, "Source state:"+srcPos+", Target state:"+dstPos);
		return srcPos==-1||dstPos==-1? false: srcPos==dstPos;
	}

	@Override
	protected void onStartPlugin(Context context) {
		if (DEBUG) Log.d(TAG, "Starting EvalConditionService...");
		if (!EvalConditionService.isStarted()) {
			context.startService(new Intent(context, EvalConditionService.class));
		}
	}

	@Override
	protected void onStopPlugin(Context context) {
		if (DEBUG) Log.d(TAG, "Stopping EvalConditionService...");
		context.stopService(new Intent(context, EvalConditionService.class));
	}

	@Override
	protected int onQueryPluginStatus(Context context) {
		return EvalConditionService.isStarted()? STATUS_STARTED: STATUS_STOPPED;
	}

}
